<?php
namespace Admin\Controller;
class FinanceController extends CommonController{
	public $exec_comfunc  = 'index';//index,add,edit,delete

	public function index(){
		//按用户类型搜索
		$user_type = $this->_get('user_type','','teacher');
		if($user_type){
			$where['user_type'] = $user_type;
		}
		$this->assign('user_type',$user_type);
		
		//按账单类型搜索
		$bill_type = $this->_get('bill_type');
		if($bill_type){
			$where['bill_type'] = $bill_type;
		}
		$this->assign('bill_type',$bill_type);

		//按交易方式搜索
		$pay_type = $this->_get('pay_type');
		if($pay_type){
			$where['pay_type'] = $pay_type;
		}
		$this->assign('pay_type',$pay_type);
		
		//按关键字类型
		$kw_type = $this->_get('kw_type');
		$k_word = $this->_get('k_word');
		
		$this->assign('kw_type',$kw_type);
		$this->assign('k_word',$k_word);

		if($kw_type && $k_word){
			switch ($kw_type) {
				case 'order_sn':
					$where['order_sn'] = $k_word;
					break;
				
				case 'bill_id':
					$where['bill_id'] = $k_word;
					break;

				case 'realname':
					$uwhere['realname'] = array('like','%' . $k_word . '%');
					$model = $user_type == 'teacher' ? 'Teacher' : 'Student';
					$user_id_list = D($model)->where($uwhere)->getField('user_id', true);

					$where['user_id'] = array('in',implode(',',$user_id_list));
					break;

				//按手机号搜索
				case 'mobile':
					$uwhere['mobile'] = $k_word;
					$where['user_id'] = D('User')->where($uwhere)->getField('user_id');
					break;

				//按用户ID
				case 'user_id':
					$where['user_id'] = $k_word;
					break;
			}
		}


		//按账户类型搜索
		$account_type = $this->_get('account_type');
		if($account_type){
			$where['account_type'] = $account_type;
		}
		$this->assign('account_type',$account_type);
		

		//按开始价格
		$start_price = $this->_get('start_price');
		if($start_price){
			$where['money'][] = array('egt',$start_price);
		}
		$this->assign('start_price',$start_price);

		//按结束价格
		$end_price   = $this->_get('end_price');
		if($end_price){
			$where['money'][] = array('elt',$end_price);
		}
		$this->assign('end_price',$end_price);

		//资金流向
		$price_type = $this->_get('price_type','','all');
		$this->assign('price_type',$price_type);
		//收入
		if($price_type == 'in'){
			$where['money'] = array('gt',0);
		}
		//支出
		if($price_type == 'out'){
			$where['money'] = array('lt',0);
		}
		//时间类型
		$time_type = $this->_get('time_type');
		$this->assign('time_type',$time_type);
		switch ($time_type) {
			case 'today':
				$where['create_time'][] = array('egt',strtotime(date('Y-m-d')));
				break;

			case 'week':
				$where['create_time'][] = array('egt',strtotime('-1 week'));
				break;

			case 'month':
				$where['create_time'][] = array('egt',strtotime('-1 month'));
				break;

			case '3month':
				$where['create_time'][] = array('egt',strtotime('-3 month'));
				break;
		}
		//用户查看数据开始日期
		if($this->_view_data_start_time > 0){
			$where['create_time'][] = array('egt', $this->_view_data_start_time);
		}
		//用户查看数据结束日期
		if($this->_view_data_end_time > 0){
			$where['create_time'][] = array('elt', $this->_view_data_end_time);
		}
		//按开始日期
		$start_date = $this->_get('start_date');
		if($start_date){
			$where['create_time'][] = array('egt',strtotime($start_date));
		}
		$this->assign('start_date',$start_date);

		//按结束日期
		$end_date   = $this->_get('end_date');
		if($end_date){
			$where['create_time'][] = array('elt',strtotime($end_date.'23:59:59'));
		}
		$this->assign('end_date',$end_date);

		//终端类型
		$app_type = $this->_get('app_type');
		$this->assign('app_type',$app_type);
		if($app_type){
			$where['app_type'] = array('in', implode(',', $app_type));
			$this->app_is_ios     = in_array('ios', $app_type);
			$this->app_is_android = in_array('android', $app_type);
			$this->app_is_web     = in_array('web', $app_type);
			$this->app_is_wap     = in_array('wap', $app_type);
		}
		//支付类型
		$pay_type = $this->_get('pay_type');
		$this->assign('pay_type',$pay_type);
		if($pay_type){
			$where['pay_type'] = array('in', implode(',', $pay_type));
			$this->pay_is_alipay      = in_array('alipay', $pay_type);
			$this->pay_is_system      = in_array('system', $pay_type);
			$this->pay_is_balance_pay = in_array('balance_pay', $pay_type);
			$this->pay_is_weixin      = in_array('weixin', $pay_type);
			$this->pay_is_upmp        = in_array('upmp', $pay_type);
			$this->pay_is_kuaiqian    = in_array('kuaiqian', $pay_type);
		}
		//省份
		$province_id = $this->_get('province_id');
		$info['province_id'] = $province_id;
		if($province_id && !$city_id){
			$where['province_id'] = $province_id;
		}
		//城市
		$city_id = $this->_get('city_id');
		$info['city_id'] = $city_id;
		if($city_id){
			$where['city_id'] = $city_id;
		}
		$this->assign('info',$info);
		//客服城市
		if($this->_city_id){
			$where['city_id'] = $this->_city_id;
		}

		$export = $this->_get('export');
		if($export){
			$this->_show_all_data = true;
		}
		//页面金额汇总
		$page_price  = 0;
		$list = parent::index('UserBill',$where,'create_time desc',true);
		foreach ($list as $rs) {
			$data = D('Finance')->filter_field($rs);
			$page_price += $data['money'];
			$new_list[] = $data;
		}
		//显示内容名称
		$data_title = array(
			// 'bill_id'           => array('title'=>'流水单号','td_style'=>'','val_class'=>'','val_pre'=>'','val_default'=>'0'),
			'user_id'           => array('title'=>'UserID','td_style'=>'','val_class'=>'','val_pre'=>'','val_default'=>''),
			'realname'          => array('title'=>'姓名','td_style'=>'','val_class'=>'','val_pre'=>'','val_default'=>''),
			'account_type_name' => array('title'=>'流入账户','td_style'=>'','val_class'=>'','val_pre'=>'','val_default'=>''),
			'content'           => array('title'=>'名称/备注','td_style'=>'width:300px;text-align:left','val_class'=>'','val_pre'=>'','val_default'=>''),
			'order_sn'          => array('title'=>'订单号','td_style'=>'','val_class'=>'','val_pre'=>'','val_default'=>''),
			'bill_type_name'    => array('title'=>'交易类型','td_style'=>'','val_class'=>'','val_pre'=>'','val_default'=>''),
			'money'             => array('title'=>'金额','td_style'=>'','val_class'=>'price','val_pre'=>'','val_default'=>'0'),
			'balance'           => array('title'=>'余额','td_style'=>'','val_class'=>'price','val_pre'=>'','val_default'=>'0'),
			'datetime'          => array('title'=>'账单时间','td_style'=>'','val_class'=>'','val_pre'=>'','val_default'=>''),
			'pay_mode'          => array('title'=>'交易方式','td_style'=>'','val_class'=>'','val_pre'=>'','val_default'=>''),
			'app_type_name'     => array('title'=>'终端','td_style'=>'','val_class'=>'','val_pre'=>'','val_default'=>''),
		);
		if($export=='xls'){
			import("ExcelUtil",'Public/Class/suncco/');
			ExcelUtil::export($data_title, $new_list, ($user_type == 'teacher' ? '老师' : '学生').'账单流水清单');
		}

		// var_dump($new_list);
		//总金额汇总
		$total_price = $this->_model->where($where)->sum('money');
		$total_price = $total_price ? $total_price : 0;
		$this->assign('page_price', $page_price);
		$this->assign('total_price', $total_price);
		$this->assign('data_title', $data_title);
		$this->assign('table_width', '1400');
		$this->assign('list', $new_list);
		$this->display();
	}
	//收单管理
	public function acquiring(){
		//省份
		$province_id = $this->_get('province_id');
		$info['province_id'] = $province_id;
		if($province_id && !$city_id){
			$where['province_id'] = $province_id;
		}
		//城市
		$city_id = $this->_get('city_id');
		$info['city_id'] = $city_id;
		if($city_id){
			$where['city_id'] = $city_id;
		}
		$this->assign('info',$info);
		//客服城市
		if($this->_city_id){
			$where['city_id'] = $this->_city_id;
		}
		
		//时间类型
		$time_type = $this->_get('time_type');
		$this->assign('time_type',$time_type);
		switch ($time_type) {
			case 'today':
				$where['create_time'][] = array('egt',strtotime(date('Y-m-d')));
				break;

			case 'week':
				$where['create_time'][] = array('egt',strtotime('-1 week'));
				break;

			case 'month':
				$where['create_time'][] = array('egt',strtotime('-1 month'));
				break;

			case '3month':
				$where['create_time'][] = array('egt',strtotime('-3 month'));
				break;
		}
		//用户查看数据开始日期
		if($this->_view_data_start_time > 0){
			$where['create_time'][] = array('egt', $this->_view_data_start_time);
		}
		//用户查看数据结束日期
		if($this->_view_data_end_time > 0){
			$where['create_time'][] = array('elt', $this->_view_data_end_time);
		}
		//按开始日期
		$start_date = $this->_get('start_date');
		if($start_date){
			$where['create_time'][] = array('egt',strtotime($start_date));
		}
		$this->assign('start_date',$start_date);

		//按结束日期
		$end_date   = $this->_get('end_date');
		if($end_date){
			$where['create_time'][] = array('elt',strtotime($end_date.'23:59:59'));
		}
		$this->assign('end_date',$end_date);

		//按关键字类型
		$kw_type = $this->_get('kw_type');
		$k_word = $this->_get('k_word');
		
		$this->assign('kw_type',$kw_type);
		$this->assign('k_word',$k_word);

		if($kw_type && $k_word){
			switch ($kw_type) {
				case 'order_sn':
					$where['order_sn'] = $k_word;
					break;
				
				case 'bill_id':
					$where['bill_id'] = $k_word;
					break;

				//按手机号搜索
				case 'mobile':
					$uwhere['mobile'] = $k_word;
					$where['user_id'] = D('User')->where($uwhere)->getField('user_id');
					break;
					
				//按用户ID
				case 'user_id':
					$where['user_id'] = $k_word;
					break;

				//按用户姓名
				case 'realname':
					$uwhere['realname'] = array('like','%' . $k_word . '%');
					$user_id_list = D('Student')->where($uwhere)->getField('user_id',true);

					$where['user_id'] = array('in',implode(',',$user_id_list));
					break;
			}
		}

		//按开始价格
		$start_price = $this->_get('start_price');
		if($start_price){
			$where['total_fee'][] = array('egt',$start_price);
		}
		$this->assign('start_price',$start_price);

		//按结束价格
		$end_price   = $this->_get('end_price');
		if($end_price){
			$where['total_fee'][] = array('elt',$end_price);
		}
		$this->assign('end_price',$end_price);

		//支付用户类型
		$pay_user_type = $this->_get('pay_user_type','','all');
		$this->assign('pay_user_type',$pay_user_type);
		if($pay_user_type != 'all'){
			$where['pay_user_type'] = $pay_user_type;
		}

		//收单方式
		$order_type = $this->_get('order_type','','all');
		$this->assign('order_type',$order_type);
		//充值
		if($order_type == 'recharge_order'){
			$where['order_type'] = 'recharge_order';
		}
		//支付
		if($order_type == 'class_order'){
			$where['order_type'] = 'class_order';
		}

		//支付类型
		$pay_type = $this->_get('pay_type');
		$this->assign('pay_type',$pay_type);
		if($pay_type){
			$where['pay_type'][] = array('eq',$pay_type);
		}

		$platform_bank_id = $this->_get('platform_bank_id');
		$this->assign('platform_bank_id',$platform_bank_id);
		if($platform_bank_id){
			$where['seller_email'] = $platform_bank_id;
		}

		$export = $this->_get('export');
		if($export){
			$this->_show_all_data = true;
		}
		
		//去掉余额支付
		$where['pay_type'][] = array('neq','balance_pay');
		$where['pay_type'][] = array('neq','give');
		//页面金额汇总
		$page_price = 0;
		$data = parent::index('OrderPay',$where,'create_time desc',true);
		foreach ($data as $rs) {
			$page_price += $rs['total_fee'];
			$rs['realname']          = D('User')->get_user_name($rs['user_id']);
			$rs['account_type_name'] = in_array($rs['order_type'], array('recharge_order', 'give_order')) ? '可用余额' : '托管余额';
			$rs['order_type_desc']   = get_type_desc($rs['order_type'], 'order_type_desc');
			$rs['app_type_desc']     = get_type_desc($rs['app_type'], 'app_type_desc');
			$rs['pay_type_desc']     = get_type_desc($rs['pay_type'], 'pay_type_desc');
			$rs['datetime']          = date('Y-m-d H:i:s',$rs['create_time']);
			$list[] = $rs;
		}
		//显示内容名称
		$data_title = array(
			'order_sn'           => array('title'=>'订单号','td_style'=>'','val_class'=>'','val_pre'=>'','val_default'=>''),
			'datetime'           => array('title'=>'时间','td_style'=>'','val_class'=>'','val_pre'=>'','val_default'=>''),
			'order_type_desc'    => array('title'=>'收单方式','td_style'=>'','val_class'=>'','val_pre'=>'','val_default'=>''),
			'total_fee'          => array('title'=>'金额','td_style'=>'','val_class'=>'price','val_pre'=>'','val_default'=>'0'),
			'pay_type_desc'      => array('title'=>'支付方式','td_style'=>'','val_class'=>'','val_pre'=>'','val_default'=>''),
			'account_type_name'  => array('title'=>'流入账户','td_style'=>'','val_class'=>'','val_pre'=>'','val_default'=>''),
			'user_id'            => array('title'=>'UserID','td_style'=>'','val_class'=>'','val_pre'=>'','val_default'=>''),
			'realname'           => array('title'=>'姓名','td_style'=>'','val_class'=>'','val_pre'=>'','val_default'=>''),
			// 'app_type_desc'      => array('title'=>'终端类型','td_style'=>'','val_class'=>'','val_pre'=>'','val_default'=>''),
			'buyer_email'        => array('title'=>'支付账号','td_style'=>'','val_class'=>'','val_pre'=>'','val_default'=>''),
			'seller_email'       => array('title'=>'收款账号','td_style'=>'','val_class'=>'','val_pre'=>'','val_default'=>''),
		);
		if($this->_get('is_teacher_name')){
			$data_title['teacher_realname'] = array('title'=>'老师姓名','td_style'=>'','val_class'=>'','val_pre'=>'','val_default'=>'');
		}
		if($export=='xls'){
			import("ExcelUtil",'Public/Class/suncco/');
			ExcelUtil::export($data_title, $list, '订单流水清单');
		}
		//总金额汇总
		$total_price = $this->_model->where($where)->sum('total_fee');
		$total_price = $total_price ? $total_price : 0;
		$this->assign('page_price',$page_price);
		$this->assign('total_price',$total_price);
		$this->assign('data_title',$data_title);
		$this->assign('list',$list);
		$this->assign('table_width','1300');
		$this->display();
	}
	//用户充值
	public function recharge(){
		if($this->isPost()){
			$user_type        = $this->_post('user_type','','student');
			$user_id          = $this->_post('user_id','请选择用户！');
			$money            = $this->_post('money','请输入要充值金额！');
			$remark           = $this->_post('remark');
			$platform_bank_id = $this->_post('platform_bank_id');
			if(!is_numeric($money)){
				$this->error('您输入金额有误！');
			}
			$user_type = $user_type == 'teacher' ? 'teacher' : 'student';
			$order_sn  = D('Order')->order_sn();
			if($user_type == 'teacher'){
				$info = D('Teacher')->where(array('user_id'=>$user_id))->field('province_id,city_id')->find();
			}else{
				$info = D('Student')->where(array('user_id'=>$user_id))->field('province_id,city_id')->find();
			}
			$province_id = $info['province_id'] ? $info['province_id'] : 0;
			$city_id     = $info['city_id'] ? $info['city_id'] : 0;

			//创建充值订单
			$data['order_sn']    = $order_sn;
			$data['province_id'] = $province_id;
			$data['city_id']     = $city_id;
			$data['user_id']     = $user_id;
			$data['user_type']   = $user_type;
			$data['pay_type']    = 'cash';
			$data['amount']      = $money;
			$data['status']      = 1;
			$data['create_time'] = time();
			$data['pay_time']    = time();

			if(D('RechargeOrder')->add($data)){
				//生成充值支付流水
				unset($data);
				$data['user_id']      = $user_id;
				$data['province_id']  = $province_id;
				$data['city_id']      = $city_id;
				$data['order_type']   = 'recharge_order';
				$data['pay_type']     = 'cash';
				$data['order_sn']     = $order_sn;
				$data['trade_no']     = time();
				$data['total_fee']    = $money;
				$data['buyer_email']  = $user_id;
				$data['buyer_id']     = $user_id;
				$data['seller_email'] = $platform_bank_id ? $platform_bank_id : '';
				$data['create_time']  = time();
				if(D('OrderPay')->add($data)){
					D('Finance')->recharge($user_id,$money,$user_type,$remark);
					$this->success('充值成功');
				}
			}
			$this->error('充值失败！');
		}
		$this->display();
	}
	//赠送金额
	public function give(){
		if($this->isPost()){
			$user_type        = $this->_post('user_type','','student');
			$user_id          = $this->_post('user_id','请选择用户！');
			$money            = $this->_post('money','请输入要赠送金额！');
			$remark           = $this->_post('remark');
			$platform_bank_id = $this->_post('platform_bank_id');
			if(!is_numeric($money)){
				$this->error('您输入金额有误！');
			}
			$user_type = $user_type == 'teacher' ? 'teacher' : 'student';
			$order_sn  = D('Order')->order_sn();
			if($user_type == 'teacher'){
				$info = D('Teacher')->where(array('user_id'=>$user_id))->field('province_id,city_id')->find();
			}else{
				$info = D('Student')->where(array('user_id'=>$user_id))->field('province_id,city_id')->find();
			}
			$province_id = $info['province_id'] ? $info['province_id'] : 0;
			$city_id     = $info['city_id'] ? $info['city_id'] : 0;

			//创建充值订单
			$data['order_sn']    = $order_sn;
			$data['province_id'] = $province_id;
			$data['city_id']     = $city_id;
			$data['user_id']     = $user_id;
			$data['user_type']   = $user_type;
			$data['pay_type']    = 'give';
			$data['amount']      = $money;
			$data['status']      = 1;
			$data['create_time'] = time();
			$data['pay_time']    = time();

			if(D('RechargeOrder')->add($data)){
				//生成充值支付流水
				unset($data);
				$data['user_id']      = $user_id;
				$data['province_id']  = $province_id;
				$data['city_id']      = $city_id;
				$data['order_type']   = 'give_order';
				$data['pay_type']     = 'give';
				$data['order_sn']     = $order_sn;
				$data['trade_no']     = time();
				$data['total_fee']    = $money;
				$data['buyer_email']  = $user_id;
				$data['buyer_id']     = $user_id;
				$data['seller_email'] = $platform_bank_id ? $platform_bank_id : '';
				$data['create_time']  = time();
				if(D('OrderPay')->add($data)){
					D('Finance')->recharge($user_id, $money, $user_type, $remark, '', 'give');
					$this->success('赠送成功');
				}
			}
			$this->error('赠送失败！');
		}
		$this->display();
	}
	//提现
	public function take_out(){
		//按用户类型搜索
		$user_type = $this->_get('user_type','','teacher');
		if($user_type){
			$where['user_type'] = $user_type;
		}
		$this->assign('user_type',$user_type);

		//省份
		$province_id = $this->_get('province_id');
		$info['province_id'] = $province_id;
		if($province_id && !$city_id){
			$where['province_id'] = $province_id;
		}
		//城市
		$city_id = $this->_get('city_id');
		$info['city_id'] = $city_id;
		if($city_id){
			$where['city_id'] = $city_id;
		}
		//客服城市
		if($this->_city_id){
			$where['city_id'] = $this->_city_id;
		}

		//时间类型
		$time_type = $this->_get('time_type');
		$this->assign('time_type',$time_type);
		switch ($time_type) {
			case 'today':
				$where['create_time'][] = array('egt',strtotime(date('Y-m-d')));
				break;

			case 'week':
				$where['create_time'][] = array('egt',strtotime('-1 week'));
				break;

			case 'month':
				$where['create_time'][] = array('egt',strtotime('-1 month'));
				break;

			case '3month':
				$where['create_time'][] = array('egt',strtotime('-3 month'));
				break;
		}
		//用户查看数据开始日期
		if($this->_view_data_start_time > 0){
			$where['create_time'][] = array('egt', $this->_view_data_start_time);
		}
		//用户查看数据结束日期
		if($this->_view_data_end_time > 0){
			$where['create_time'][] = array('elt', $this->_view_data_end_time);
		}
		//按开始日期
		$start_date = $this->_get('start_date');
		if($start_date){
			$where['create_time'][] = array('egt',strtotime($start_date));
		}
		$this->assign('start_date',$start_date);

		//按结束日期
		$end_date   = $this->_get('end_date');
		if($end_date){
			$where['create_time'][] = array('elt',strtotime($end_date.'23:59:59'));
		}
		$this->assign('end_date',$end_date);

		//按审核开始日期
		$audit_start_date = $this->_get('audit_start_date');
		if($audit_start_date){
			$where['update_time'][] = array('egt',strtotime($audit_start_date));
		}
		$this->assign('audit_start_date',$audit_start_date);

		//按审核结束日期
		$audit_end_date   = $this->_get('audit_end_date');
		if($audit_end_date){
			$where['update_time'][] = array('elt',strtotime($audit_end_date.'23:59:59'));
		}
		$this->assign('audit_end_date',$audit_end_date);


		//按关键字类型
		$kw_type = $this->_get('kw_type');
		$k_word = $this->_get('k_word');
		
		$this->assign('kw_type',$kw_type);
		$this->assign('k_word',$k_word);

		if($kw_type && $k_word){
			switch ($kw_type) {
				//按用户ID搜索
				case 'user_id':
					$where['user_id'] = $k_word;
					break;
				
				//按用户姓名
				case 'realname':
					$uwhere['realname'] = array('like','%' . $k_word . '%');
					$model = $user_type == 'teacher' ? 'Teacher' : 'Student';
					$user_id_list = D($model)->where($uwhere)->getField('user_id',true);

					$where['user_id'] = array('in',implode(',',$user_id_list));
					break;

				//按手机号搜索
				case 'mobile':
					$uwhere['mobile'] = $k_word;
					$where['user_id'] = D('User')->where($uwhere)->getField('user_id');
					break;
			}
		}
		//按开始价格
		$start_price = $this->_get('start_price');
		if($start_price){
			$where['money'][] = array('egt',$start_price);
		}
		$this->assign('start_price',$start_price);

		//按结束价格
		$end_price   = $this->_get('end_price');
		if($end_price){
			$where['money'][] = array('elt',$end_price);
		}
		$this->assign('end_price',$end_price);

		//按状态搜索
		$platform_bank_id = $this->_get('platform_bank_id');
		if($platform_bank_id){
			$where['platform_bank_id'] = $platform_bank_id;
		}
		$this->assign('platform_bank_id',$platform_bank_id);

		//按状态搜索
		$status = $this->_get('status');
		if($status){
			$where['status'] = $status;
		}
		$this->assign('status',$status);

		//按状态搜索
		$bank_type = $this->_get('bank_type');
		if($bank_type){
			$where['bank_type'] = $bank_type;
		}
		$this->assign('bank_type',$bank_type);

		//终端类型
		$app_type = $this->_get('app_type');
		$this->assign('app_type',$app_type);
		if($app_type){
			$where['app_type'] = array('in', implode(',', $app_type));
			$this->app_is_ios     = in_array('ios', $app_type);
			$this->app_is_android = in_array('android', $app_type);
			$this->app_is_web     = in_array('web', $app_type);
			$this->app_is_wap     = in_array('wap', $app_type);
			$this->app_is_system  = in_array('system', $app_type);
		}

		//导出清单处理
		$export = $this->_get('export');
		if($export){
			$this->_show_all_data = true;
		}

		$page_price = 0;
		$list = parent::index('UserTakeOut', $where, 'create_time desc', true);
		// var_dump($this->_model->getLastSql());
		foreach ($list as $rs) {
			$page_price += $rs['money'];
			$rs = D('Finance')->take_out_filter($rs);
			if($export){
				unset($where);
				$where['card_id'] = $rs['card_id'];
				$card_info = D('UserBankCard')->where($where)->find();
				$rs['bank_name']      = D('Category')->get_name($card_info['card_type']);
				$rs['card_no']        = $card_info['card_no'];
				$rs['account_name']   = $card_info['account_name'];
				$rs['bank_city_name'] = D('Area')->id2names($card_info['city_id']);
				$rs['open_bank_name'] = $card_info['open_bank_name'];
			}
			$new_list[] = $rs;
		}

		//显示内容名称
		//银行名称、卡号、账户名、所属城市、开户行名称、提现金额
		$data_title = array(
			'id'                 => array('title'=>'ID','td_style'=>'','val_class'=>'','val_pre'=>'','val_default'=>''),
			'user_id'            => array('title'=>'UserID','td_style'=>'','val_class'=>'','val_pre'=>'','val_default'=>''),
			'realname'           => array('title'=>'姓名','td_style'=>'','val_class'=>'','val_pre'=>'','val_default'=>''),
			'money'              => array('title'=>'提现金额','td_style'=>'','val_class'=>'price','val_pre'=>'','val_default'=>''),
			'datetime'           => array('title'=>'提现时间','td_style'=>'','val_class'=>'','val_pre'=>'','val_default'=>''),
			'platform_bank_name' => array('title'=>'提现方式','td_style'=>'','val_class'=>'platform_bank_name','val_pre'=>'','val_default'=>''),
			'status_desc'        => array('title'=>'审核状态','td_style'=>'','val_class'=>'status_desc','val_pre'=>'','val_default'=>'0'),
			'update_time_desc'   => array('title'=>'审核时间','td_style'=>'','val_class'=>'update_time_desc','val_pre'=>'','val_default'=>''),
			'admin_name'         => array('title'=>'审核人','td_style'=>'','val_class'=>'admin_name','val_pre'=>'','val_default'=>''),
			'bank_name'          => array('title'=>'银行名称','td_style'=>'','val_class'=>'','val_pre'=>'','val_default'=>'','no_show'=>true),
			'card_no'            => array('title'=>'卡号','td_style'=>'','val_class'=>'','val_pre'=>'','val_default'=>'','no_show'=>true),
			'account_name'       => array('title'=>'账户名','td_style'=>'','val_class'=>'','val_pre'=>'','val_default'=>'','no_show'=>true),
			'bank_city_name'     => array('title'=>'所属城市','td_style'=>'','val_class'=>'','val_pre'=>'','val_default'=>'','no_show'=>true),
			'open_bank_name'     => array('title'=>'开户行名称','td_style'=>'','val_class'=>'','val_pre'=>'','val_default'=>'','no_show'=>true),
			// 'app_type_name'      => array('title'=>'终端类型','td_style'=>'','val_class'=>'','val_pre'=>'','val_default'=>''),
		);
		if($export=='xls'){
			import("ExcelUtil",'Public/Class/suncco/');
			ExcelUtil::export($data_title, $new_list, '用户提现清单');
		}
		//总金额汇总
		$total_price = $this->_model->where($where)->sum('money');
		$total_price = $total_price ? $total_price : 0;
		$this->assign('page_price',$page_price);
		$this->assign('total_price',$total_price);
		$this->assign('data_title',$data_title);
		// $this->assign('table_width','1200');
		$this->assign('info',$info);
		$this->assign('list',$new_list);
		$this->display();
	}
	public function win_take_out(){
		$take_out_id = $this->_get('id','您的操作有误！');
		$page        = $this->_get('page');

		$where['id'] = $take_out_id;
		$info = D('UserTakeOut')->where($where)->find();
		if(IS_POST){
			$money            = $info['money'];
			$user_id          = $info['user_id'];
			$user_type        = $info['user_type'];
			$status           = $this->_post('status');
			$platform_bank_id = $this->_post('platform_bank_id');
			$trade_no         = $this->_post('trade_no');
			$remark           = $this->_post('remark');
			$admin_id         = $this->_user_id;
			//确认记录是否有处理过，查询时锁住
			if(D('UserTakeOut')->lock(true)->where($where)->getField('status') == 1){

				//拒绝提现，将金额返还到账户余额中
				if($status == 3){
					D('Finance')->take_out_refund($user_id, $money, $remark);
				}

				$data['admin_id']         = $admin_id;
				$data['trade_no']         = $trade_no;
				$data['platform_bank_id'] = $platform_bank_id;
				$data['remark']           = $remark;
				$data['update_time']      = time();
				$data['status']           = $status;
				D('UserTakeOut')->where($where)->save($data);
				// if($status == 2){
				// 	//提现成功消息推送
				// 	D('Message')->bill_msg_push($take_out_id, 'take_out_doing');
				// }
				// if($status == 3){
				// 	//退款消息推送
				// 	D('Message')->bill_msg_push($take_out_id, 'take_out_error');
				// }
			}
			$json['url'] = U('take_out', array('p' => $page));
			$json['platform_bank_name'] = D('Category')->get_name($platform_bank_id);
			$json['status_desc']        = get_type_desc($status, 'take_out_status_desc');
			$json['update_time_desc']   = date('Y-m-d H:i:s', time());
			$json['admin_name']         = $this->_username;

			$this->json_success('操作成功！',$json);
		}

		unset($where);
		$where['user_id'] = $info['user_id'];
		$user_info = D('User')->where($where)->find();

		$where['card_id'] = $info['card_id'];
		$bank_carid = D('UserBankCard')->where($where)->find();
		$bank_carid['bank_name'] = D('Category')->get_name($bank_carid['card_type']);
		$bank_carid['city_name'] = D('Area')->id2names($bank_carid['city_id'],true);

		$info['admin_name'] = D('User')->get_admin_name($info['admin_id']);

		$this->assign('info',$info);
		$this->assign('user_info',$user_info);
		$this->assign('bank_carid',$bank_carid);
		$this->display();
	}
	//提现到银行卡
	public function take_out_to_bank(){
		$take_out_id = $this->_get('take_out_id', '对不起，您的操作有误！');
		$result = D('Finance')->take_out_to_bank($take_out_id);
		if($result['status'] != '200'){
			$this->json_error($result['info']);
		}
		$this->json_success('提交成功！', $result['data']);
	}
	//提现到银行卡
	public function take_out_status_query(){
		$take_out_id = $this->_get('take_out_id', '对不起，您的操作有误！');
		$result = D('Finance')->take_out_status_query($take_out_id);
		if($result['status'] != '200'){
			$this->json_error($result['info']);
		}
		$this->json_success($result['info']);
	}
	//修改提现备注
	public function edit_take_out_remark(){
		if($this->isPost()){
			$take_out_id = $this->_get('take_out_id', '对不起，您的操作有误！');
			$content = $this->_post('content');

			$where['id'] = $take_out_id;
			$data['remark'] = $content;

			D('UserTakeOut')->where($where)->save($data);

			$this->json_success('修改成功！');
		}
		$this->json_error('对不起，您的操作有误！');
	}

}

?>